﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Ordinaire
{
    /// <summary>
    /// Provides a static instance of System.ComponentModel.Composition.Hosting.CompositionContainer.
    /// </summary>
    public class ExtensionContainer
    {
        #region Variables

        /// <summary>
        /// Stores composition catalogs.
        /// </summary>
        private static readonly Lazy<AggregateCatalog> aggregateCatalog = new Lazy<AggregateCatalog>();

        /// <summary>
        /// Static instance of CompositionContainer.
        /// </summary>
        private static CompositionContainer compositionContainer;

        #endregion

        #region Constructor

        /// <summary>
        /// Hides constructor.
        /// </summary>
        private ExtensionContainer() {}

        #endregion

        #region Public methods

        /// <summary>
        /// Adds specified type(s) as composition catalogs.
        /// </summary>
        /// <param name="types">types of assembly to add</param>
        public static void Add(params Type[] types)
        {
            Assembly[] assemblies = types.AsEnumerable().Select(d => d.Assembly).ToArray();
            Add(assemblies);
        }

        /// <summary>
        /// Adds specified assembly(es) as composition catalogs.
        /// </summary>
        /// <param name="assemblies">assemblies to add</param>
        public static void Add(params Assembly[] assemblies)
        {
            // Dispose existing CompositionContainer
            if (compositionContainer != null) 
            {
                compositionContainer.Dispose();
                compositionContainer = null;
            }

            Assembly assembly = null;
            int assembliesLength = assemblies.Length;
            for (int i = 0; i < assembliesLength; i++)
            {
                assembly = assemblies[i];
                aggregateCatalog.Value.Catalogs.Add(new AssemblyCatalog(assembly));
            }
        }

        /// <summary>
        /// Adds specified paths as composition catalogs.
        /// </summary>
        /// <param name="paths">paths to add</param>
        public static void Add(params string[] paths)
        {
            // Dispose existing CompositionContainer
            if (compositionContainer != null) 
            {
                compositionContainer.Dispose();
                compositionContainer = null;
            }

            string path = String.Empty;
            int pathsLength = paths.Length;
            for (int i = 0; i < pathsLength; i++)
            {
                path = paths[i];
                aggregateCatalog.Value.Catalogs.Add(new DirectoryCatalog(path));
            }
        }

        /// <summary>
        /// Returns static instance of CompositionContainer with the added catalogs.
        /// </summary>
        public static System.ComponentModel.Composition.Hosting.CompositionContainer Instance
        {
            get
            {
                if (compositionContainer == null)
                {
                    compositionContainer = new CompositionContainer(aggregateCatalog.Value);
                }
                return compositionContainer;
            }
        }

        #endregion
    }
}
